package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    /**
     * 后台修改频道信息
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //1.校验参数
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2.判断频道是否存在
        if (channelIsExist(wmChannel)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        wmChannel.setCreatedTime(new Date());
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult("频道修改成功！");
    }

    /**
     * 自媒体频道列表查询
     * @return
     */
    @Override
    public ResponseResult findAllChannel() {
        //1.查询全部频道
        List<WmChannel> wmChannelList = wmChannelMapper.selectList(null);
        return ResponseResult.okResult(wmChannelList);
    }

    /**
     * 后台管理平台频道查询
     * @return
     */
    @Override
    public ResponseResult findChannel(ChannelDto channelDto) {
        //1.校验参数
        if (channelDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2.创建查询条件
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        Page<WmChannel> wmChannelPage = new Page<>(channelDto.getPage(), channelDto.getSize());
        if (channelDto.getName() != null){
            queryWrapper.like(WmChannel::getName,channelDto.getName());
        }
        wmChannelMapper.selectPage(wmChannelPage,queryWrapper);

        PageResponseResult result = new PageResponseResult(channelDto.getPage(),channelDto.getSize(), (int) wmChannelPage.getTotal());
        result.setData(wmChannelPage.getRecords());
        return result;

    }

    /**
     * 后台保存自媒体品频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        //1.参数校验
        if (wmChannel == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (wmChannel.getStatus() == null || wmChannel.getName()==null || wmChannel.getDescription()==null || wmChannel.getOrd()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.判断频道是否存在
        if (channelIsExist(wmChannel)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //3.封装数据
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);
        wmChannelMapper.insert(wmChannel);
        return ResponseResult.okResult("新增频道成功！");
    }

    /**
     * 判断频道是否存在
     * @param wmChannel
     * @return
     */
    private boolean channelIsExist(WmChannel wmChannel) {
        List<WmChannel> wmChannels = wmChannelMapper.selectList(null);
        List<String> strings = wmChannels.stream().map(wmChannel1 -> {
            return wmChannel1.getName();
        }).collect(Collectors.toList());

        for (String string : strings) {
            if (string.equals(wmChannel.getName())){
                return true;
            }
        }
        return false;
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //校验参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.判断该频道是否有文章：在文章表中根据怕频道id查询是否存在文章
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNews::getChannelId,id);
        List<WmNews> wmNews = wmNewsMapper.selectList(queryWrapper);
        if (wmNews.size()>0){
            //该频道下存在文章，不能删除
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道存在文章，无法删除");
        }

        wmChannelMapper.deleteById(id);
        return ResponseResult.okResult("删除频道成功！");
    }
}
